<?php
namespace app\frontend\controller;
use think\Db;
use think\Controller;
class Porder extends Controller
{
    public function index()
    {
    	 if(!session('user_name'))
        {
            $user_name='登录';
            $user_register='注册';
            $user_logout='';
        }
        else
        {
        $user_name=session('user_name');
        $user_register='欢迎你';
        $user_logout='退出';
        }

        $this->assign('user_name',$user_name);
        $this->assign('user_register',$user_register);
        $this->assign('user_logout',$user_logout);

    	$user_id=session('user_id');
        //----所有订单s----
        $date=db('order')->where("user_id='{$user_id}' and status!='2'")->select();
        //dump($date[0]['shopcart_id']);
        if(count($date)!=0)
        {          
           for($i=0;$i<count($date);$i++)
         {
         	$alldate=$date[$i];

         	$shopcart_id=$alldate['shopcart_id'];
         	$order_number=$alldate['order_number'];
         	$order_date=$alldate['creat_time'];
		    $shopcartId=explode(',', $shopcart_id);
		    $text=[]; 

		      for($j=0;$j<count($shopcartId);$j++)
		      {
		        if($shopcartId[$j]!='')
		        {
			        $shopcartNewid=$shopcartId[$j];
			        $data=db('shopcart')->alias('a')->join('order b','a.user_id=b.user_id')->join('product c','c.product_number=a.product_number')->where("a.shopcart_id='{$shopcartNewid}' and b.order_number='{$order_number}'")->select();
		       		        
			       foreach ($data as $val) 
			        {
			        	if($val['status']=='0')
			        	{
			        		$val['status']='未付款';
			        	}
			        	else
			        	{
			        		$val['status']='已付款';
			        	}
			         $text[]=$val; 
			        }
		        }
		      }
			   $allbox[]=$text;
			   $all[]=$order_number;
			   $orderDate[]=$order_date;   	
		 }
		
		}
		else
		{
			$allbox=[];
			$all=[];
			$orderDate=[];
		}
		 $this->assign('all',$all);
		 $this->assign('allbox',$allbox);
		 $this->assign('orderDate',$orderDate);
		 
         //----所有订单e----
         //----未付款订单s----
		 $date1=db('order')->where("user_id='{$user_id}' and status='0'")->select();
        if(count($date1)!=0)
        {
           for($i=0;$i<count($date1);$i++)
         {
         	$alldate1=$date1[$i];         	
         	$shopcart_id1=$alldate1['shopcart_id'];
         	$order_number1=$alldate1['order_number'];
         	$order_date1=$alldate1['creat_time'];
		    $shopcartId1=explode(',', $shopcart_id1);
		    $text1=[]; 

		      for($j=0;$j<count($shopcartId1);$j++)
		      {
		        if($shopcartId1[$j]!='')
		        {
			        $shopcartNewid1=$shopcartId1[$j];
			        $data1=db('shopcart')->alias('a')->join('order b','a.user_id=b.user_id')->join('product c','c.product_number=a.product_number')->where("a.shopcart_id='{$shopcartNewid1}' and b.order_number='{$order_number1}'")->select();
			      	       		        
			          foreach ($data1 as $val1) 
			        {
	        		 $val1['status']='未付款';	
			         $text1[]=$val1;			         
			        }
		        }
			  }			  
			   $allbox1[]=$text1;
			   $all1[]=$order_number1;
			   $orderDate1[]=$order_date1;
		 }
		 
		}
		else
		{
			$allbox1=[];
			$all1=[];
			$orderDate1=[];
		}
		$this->assign('all1',$all1);
		$this->assign('allbox1',$allbox1);
		$this->assign('orderDate1',$orderDate1);
		 
		//----未付款订单e----
		//----已付款订单s----
		  $date2=db('order')->where("user_id='{$user_id}' and status='1'")->select();
		  if(count($date2)!=0)
        {
   
           for($i=0;$i<count($date2);$i++)
         {
         	$alldate2=$date2[$i];         	
         	$shopcart_id2=$alldate2['shopcart_id'];
         	$order_number2=$alldate2['order_number'];
         	$order_date2=$alldate2['creat_time'];
		    $shopcartId2=explode(',', $shopcart_id2);
		    $text2=[]; 

		      for($j=0;$j<count($shopcartId2);$j++)
		      {
		        if($shopcartId2[$j]!='')
		        {
			        $shopcartNewid2=$shopcartId2[$j];
			       
			        $data2=db('shopcart')->alias('a')->join('order b','a.user_id=b.user_id')->join('product c','c.product_number=a.product_number')->where("a.shopcart_id='{$shopcartNewid2}' and b.order_number='{$order_number2}'")->field('b.*,b.shopcart_id as bname,a.*,c.*')->select();

			          foreach ($data2 as $val2) 
			        {
	        		 
	        		 if($val2['product_refund']==0)
	        		 {
	        		 	$val2['product_refund']='退款';
	        		 	$val2['status']='0';
	        		 }
	        		 else if($val2['product_refund']==1)
	        		 {
	        		 	$val2['product_refund']='退款中';
	        		 	$val2['status']='1';
	        		 }
	        		 else if($val2['product_refund']==2)
	        		 {
	        		 	$val2['product_refund']='退款成功';
	        		 	$val2['status']='2';
	        		 }
	        		 else
	        		 {
	        		 	$val2['product_refund']='退款失败';
	        		 	$val2['status']='3';
	        		 }	
			         $text2[]=$val2;    
			        }
		        }
			  }
			   $allbox2[]=$text2;
			   $all2[]=$order_number2;
			   $orderDate2[]=$order_date2;
		 }
		 
		}
		 else
		{
			$allbox2=[];
			$all2=[];
			$orderDate2=[];
		}
		 $this->assign('all2',$all2);
		 $this->assign('allbox2',$allbox2);
		 $this->assign('orderDate2',$orderDate2);
		 
		 //----已付款订单e----

       
    	return view('./person/porder');
    }
    public function refund()
    {
    	$user_id=session('user_id');
    	$shopcart_id=input('shopcart_id');
    	$order_number=input('order_number');
    	$refund_sum=input('product_subtotal');
    	$refund_number = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);

    	$data=[
          'user_id'=>$user_id,
          'shopcart_id'=>$shopcart_id,
          'order_number'=>$order_number,
          'refund_number'=> $refund_number,
          'refund_time'=>time(),
          'refund_sum'=>$refund_sum
          
         ];
         $re=db('refund')->insert($data);
         $ret=db('shopcart')->where("shopcart_id='{$shopcart_id}'")->update(['product_refund'=>'1']);

    	return true;
	}
	public function ajax()
    {
    	$user_id=session('user_id');
    	$list=db('shopcart')->where("user_id='{$user_id}' and status='0'")->select();
    	echo count($list);

    }
    public function delete()
    {
    	$order_id=input('order_id');
    	
    	if($order_id!="")
      {
        $ret=db('order')->where("order_id={$order_id}")->update(['status'=>'2']);
        if($ret==false)
        {
          $this->error('删除失败');
        }
          $this->success('删除成功');

      }

    }
    public function cancle()
    {
    	$order_id=input('order_id');
    	
    	if($order_id!="")
      {
        $ret=db('order')->where("order_id={$order_id}")->delete();
        if($ret==false)
        {
          $this->error('取消订单失败');
        }
          $this->success('取消订单成功');

      }

    }
 }

